﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using Kugar.Core.WinForm.ExtMethod;
using Kugar.Core.ExtMethod;
using Kugar.ERPFramework.ClientBroker;

namespace Kugar.ERPFramework.View.WinForm
{
    public partial class MainFormBaseStyle1 : Office2007Form
    {
        private Dictionary<SuperTabItem,Form> _cacheForm=new Dictionary<SuperTabItem, Form>();

        public MainFormBaseStyle1()
        {
            WinForm.ClientBroker.MainForm = this;

            ClientBroker.ViewDataRequest += ClientBroker_ViewDataRequest;
            ClientBroker.ViewModuleFormRequest += ClientBroker_ViewModuleFormRequest;

            InitializeComponent();
        }

        protected void ViewForm(Form frm,bool isActived=true)
        {
            this.Invoke(new MethodInvoker(() =>
                {
                    var tab = createNewTab(frm.Text);

                    frm.MdiParent = this;
                    frm.Parent = tab.AttachedControl;
                    frm.FormBorderStyle = FormBorderStyle.None;
                    frm.WindowState = FormWindowState.Normal;
                    frm.Size = new Size(tab.AttachedControl.Width, tab.AttachedControl.Height);
                    frm.Dock=DockStyle.Fill;

                    this.tabCtrlMain.Tabs.Add(tab);

                    tab.AttachedControl.Controls.Add(frm);

                    _cacheForm.Add(tab,frm);

                    if (isActived)
                    {
                        this.tabCtrlMain.SelectedTab = tab;
                    }
                }));
        }

        protected IClientBroker ClientBroker { get { return WinForm.ClientBroker.GetInstance(); } }

        private SuperTabItem createNewTab(string title)
        {
            var tabItem = new SuperTabItem();
            tabItem.Text = title;
            tabItem.CloseButtonVisible = true;
            

            var panel = new SuperTabControlPanel();

            //panel.Controls.Add(this.tabControlMainPage);
            panel.Dock = System.Windows.Forms.DockStyle.Fill;
            panel.Location = new System.Drawing.Point(0, 30);
            panel.Name = title;
            panel.Size = new Size(905, 542);
            panel.TabIndex = 1;
            panel.TabItem = tabItem;
            panel.ColorSchemeStyle = eDotNetBarStyle.StyleManagerControlled;
            

            tabItem.AttachedControl = panel;

            return tabItem;
        }

        private void tabCtrlMain_TabItemClose(object sender, SuperTabStripTabItemCloseEventArgs e)
        {
            if (e.Tab==tabItemMainPage)
            {
                e.Cancel = true;
                return;
            }

            var frm = _cacheForm.TryGetValue((SuperTabItem)e.Tab, null);

            if (frm==null)
            {
                return;
            }

            frm.Close();

            bool bResult = false;
            foreach (Form item in Application.OpenForms)
            {
                if (item == frm)
                {
                    bResult = true;
                    break;
                }
            }

            e.Cancel = bResult;
        }

        void ClientBroker_ViewModuleFormRequest(object sender, ViewModuleEventArgs e)
        {
            var frm = (Form)ClientBroker.GetModuleForm(e.ModuleFormName, e.Args);

            if (frm == null)
            {
                return;
            }
            else
            {
                this.ViewForm(frm, true);
            }
        }

        void ClientBroker_ViewDataRequest(object sender, ViewDataEventArgs e)
        {
            var frmName = ClientBroker.GetEntityFormName(e.Entity.GetType());

            if (string.IsNullOrWhiteSpace(frmName))
            {
                return;
            }
            else
            {
                var frm = (Form)ClientBroker.GetModuleForm(frmName, e.Entity);
                if (frm != null)
                {
                    ViewForm(frm, true);
                }
            }

        }

        private void MainFormBaseStyle1_Load(object sender, EventArgs e)
        {
            toolStatusLblServer.Text = string.Format("{0}:{1}", ClientBroker.ServerIP.Host, ClientBroker.ServerIP.Port);
        }
    }

    
}
